package cn.elricyu.kf.bean;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.repackaged.com.google.common.util.Base64;

public class KeyBean {
	private static String SEPERATOR = ",";

	private String appName;

	private String site;

	private int count;

	private String userId;

	private long startTime;

	private long endTime;

	private long now;

	public KeyBean(String snCode) {
		if (!StringUtils.isBlank(snCode)) {
			String[] snArray = snCode.split(SEPERATOR);
			if (snArray != null && snArray.length == 8) {
				String order = snArray[7].trim();
				int[] orderArray = new int[order.length()];
				for (int i = 0; i < order.length(); i++) {
					int pos = order.charAt(i) - '0';
					orderArray[i] = pos;
				}
				appName = snArray[orderArray[1]];
				site = snArray[orderArray[2]];
				userId = Base64Decode(snArray[orderArray[3]]);
				try {
					count = Integer.parseInt(snArray[orderArray[4]]);
				} catch (Exception e) {
				}
				try {
					startTime = Long.parseLong(snArray[orderArray[5]]);
				} catch (Exception e) {
				}
				try {
					endTime = Long.parseLong(snArray[orderArray[6]]);
				} catch (Exception e) {
				}
				now = System.currentTimeMillis();
			}
		}
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public long getNow() {
		return now;
	}

	public void setNow(long now) {
		this.now = now;
	}

	private String Base64Decode(String str) {
		try {
			return new String(Base64.decode(str), "utf-8");
		} catch (Exception e) {
			return str;
		}
	}
}
